package com.chenfan.filestore;

import com.chenfan.ccp.common.result.R;
import com.chenfan.filestore.config.DynamicDataSourceRegister;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * @author senry
 * @date 2018-11-20
 */
@Import({DynamicDataSourceRegister.class})
@EnableFeignClients(basePackages={"com.chenfan.*"})
@MapperScan(value = "com.chenfan.**.repository.**")
@EnableDiscoveryClient
@EnableTransactionManagement
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableAsync
public class FileStoreApp {
    private static final Logger logger = LoggerFactory.getLogger(FileStoreApp.class);

    public static void main(String[] args) {
        R.SUCCESS = 200;
        SpringApplication.run(FileStoreApp.class, args);
    }

    /**
     * 文件上传配置
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/data/tmp";
        logger.info("当前临时文件目录地址为：{}", location);
        File file = new File(location);
        if (!file.exists()) {
            file.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}
